﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraNavBar;
using Ultilities;
using System.Drawing;

namespace SalonManager
{
    public partial class FrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public FrmMain()
        {
            InitializeComponent();

            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.BonusSkins).Assembly);
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.OfficeSkins).Assembly);
            btnAdd.ItemClick += btnAdd_ItemClick;
            btnEdit.ItemClick += btnEdit_ItemClick;
            btnSave.ItemClick += btnSave_ItemClick;
            btnDelete.ItemClick += btnDelete_ItemClick;
            btnPrint.ItemClick += btnPrint_ItemClick;
            btnRefresh.ItemClick += btnRefesh_ItemClick;
            narBarControl.LinkPressed += narBarControl_LinkPressed;

            tabMain.SelectedPageChanged += tabMain_SelectedPageChanged;

        }

        void tabMain_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (tabMain.TabPages.Count > 0)
            {
                var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                if (frm != null)
                {
                    SetToolBarAction(sender, frm.GetCurrentToolBar());
                }
            }            
        }        

        void narBarControl_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            NavBarItem obj = ((NavBarControl)sender).PressedLink.Item;
            switch (obj.Name)
            {
                #region Main menu

                case MenuCollection.ITEM_PAYMENT:
                    //OpenTabPage(obj.Name, new M_Payments());
                    break;
                case MenuCollection.ITEM_IMPORT:
                    //OpenTabPage(obj.Name, new M_frmPurchase());
                    break;
                case MenuCollection.ITEM_WAREHOUSE:
                    break;

                #endregion

                #region Catalog

                case MenuCollection.ITEM_MATERIAL:
                    //OpenTabPage(obj.Name, new CA_Materials());
                    break;
                case MenuCollection.ITEM_CATALOG_IMPORT:
                    break;
                case MenuCollection.ITEM_CATALOG_BILL:
                    break;
                case MenuCollection.ITEM_PRODUCT_TYPE:
                    OpenTabPage(obj.Name, new FrmProductType());
                    break;
                case  MenuCollection.ITEM_SERVICE_TYPE:
                    OpenTabPage(obj.Name, new FrmServiceType());
                    break;

                #endregion

                #region Setting

                case MenuCollection.ITEM_CHANGE_USER_STYLE:
                    OpenTabPage(obj.Name, new FrmChangeUserStyle());
                    break;

                #endregion
            }            
        }

        #region Tool Bar button

        void btnAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncInsert();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        

        void btnRefesh_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncRefresh();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncPrint();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncDelete();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncSave();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncEdit();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Form Event

        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                CheckForIllegalCrossThreadCalls = false;

                lblStatusMain.Caption = Name;
                barStaticItem2.Caption = Globals.UserName;

                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(Properties.Settings.Default.SkinStyle);
            }
            catch (Exception ex)
            {
                Globals.ShowMessageBox(ex.Message, Text);
            }
        }

        #endregion

        #region Tab Main

        private void OpenTabPage(string tabName, BaseForm frm)
        {
            int index = CheckExists(tabName);
            if (index < 0)
            {
                var tabPage = new DevExpress.XtraTab.XtraTabPage {Name = tabName, Text = frm.Text};
                frm.ControlBox = false;
                frm.TopLevel = false;
                frm.Parent = tabPage;
                frm.Show();
                frm.Dock = DockStyle.Fill;
                frm.StatusBarTextChanged += actionForm_StatusBarTextChanged;
                frm.SelectedTabChanged += SetToolBarAction;
                tabMain.TabPages.Add(tabPage);
                tabMain.SelectedTabPageIndex = tabMain.TabPages.Count - 1;
                
            }
            else
            {
                tabMain.SelectedTabPageIndex = index;
            }
        }

        private int CheckExists(string tabName)
        {
            for (int i = 0; i < tabMain.TabPages.Count; i++)
            {
                if (tabMain.TabPages[i].Name == tabName)
                {
                    return i;
                }
            }

            return -1;
        }

        private void actionForm_StatusBarTextChanged(object sender, StatusBarEventArgs e)
        {
            lblStatusMain.Caption = e.StatusText;
            if (e.StatusText == Messages.STATUS_INSERT || e.StatusText == Messages.STATUS_LOADING || e.StatusText == Messages.STATUS_UPDATE)
            {
                prbMain.Visibility = BarItemVisibility.Always;
            }
            else
            {
                prbMain.Visibility = BarItemVisibility.Never;
            }

            if (e.StatusText.Equals(Messages.STATUS_ERROR) || e.StatusText.Equals(Messages.STATUS_LOADING_FAIL))
                lblStatusMain.Appearance.ForeColor = Color.Red;
            else
                lblStatusMain.Appearance.ForeColor = Color.Blue;
        }        

        private void tabMain_CloseButtonClick(object sender, EventArgs e)
        {            
            if (tabMain.TabPages.Count > 0)
            {
                var frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                if (frm != null)
                {
                    frm.Dispose();
                }
            }
            tabMain.SelectedTabPage.Dispose();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        #endregion

        #region Support Methods

        private void SetToolBarAction(object sender, TOOLBAR tb)
        {
            btnAdd.Enabled = tb.AllowAdd;
            btnEdit.Enabled = tb.AllowEdit;
            btnSave.Enabled = tb.AllowSave;
            btnDelete.Enabled = tb.AllowDelete;
            btnPrint.Enabled = tb.AllowPrint;
            btnRefresh.Enabled = tb.AllowRefresh;
        }

        #endregion
    }
}